package edu.chl.project.carshop07.ctrl.servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import edu.chl.project.carshop07.constants.Actions;
import edu.chl.project.carshop07.constants.Attributes;
import edu.chl.project.carshop07.constants.Pages;
import edu.chl.project.carshop07.constants.Patterns;
import edu.chl.project.carshop07.core.Customer;
import edu.chl.project.carshop07.core.PersonalData;
import edu.chl.project.carshop07.db.Database;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author AMADIN
 */
@WebServlet(name = "LoginServlet",
urlPatterns = {Patterns.LOGIN, Patterns.LOGOUT, Patterns.REGISTER})
public class LoginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP 
     * <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter(Attributes.ACTION);
        Customer customer = null;
        if (Actions.LOGIN.equals(action)) {

            customer = Database.getCustomer(request.getParameter(Attributes.LOGIN),
                    request.getParameter(Attributes.PASSWORD));
            if (customer == null) {
                response.sendRedirect(Pages.REGISTER);
            } else {
                request.getSession().setAttribute(Attributes.USER, customer);
                response.sendRedirect(Pages.HOME);
            }
        } else if (Actions.REGISTER.equals(action)) {
            
            String fname = request.getParameter(Attributes.FNAME);
            String lname = request.getParameter(Attributes.LNAME);
            String street = request.getParameter(Attributes.STREET);
            String city = request.getParameter(Attributes.CITY);
            String country = request.getParameter(Attributes.COUNTRY);
            String email = request.getParameter(Attributes.EMAIL);
            String regLogin = request.getParameter(Attributes.REG_LOGIN);
            String regPasswd = request.getParameter(Attributes.REG_PASSWD);
            
            
            
            PersonalData personalData = new PersonalData(street, city, country, email, lname, fname);
            customer = new Customer(regLogin, regPasswd, personalData);
            
            
            Database.addCustomer(customer);
            
            response.sendRedirect(Pages.LOGIN);
            
        } else if (Actions.LOGOUT.equals(action)) {
            request.getSession().invalidate();
            response.sendRedirect(Pages.HOME);

        } else {
            request.setAttribute(Attributes.MSG, "Could not process :" + action);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
